package leetcode;

/**
 * Created by Ghost on 2014/11/22.
 */
public class Minimum_Path_Sum {
    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][] w = new int[m][n];
        w[0][0] = grid[0][0];
        for(int i=1;i<n;i++){
            w[0][i] = w[0][i-1]+grid[0][i];
        }
        for(int i=1;i<m;i++){
            w[i][0] = w[i-1][0]+grid[i][0];
        }


        for(int i=1;i<m;i++){
            for(int j=1;j<n;j++){
                int fromUp = w[i-1][j]+grid[i][j];
                int fromLeft = w[i][j-1]+grid[i][j];
                w[i][j] = fromLeft<fromUp?fromLeft:fromUp;
            }
        }

        return w[m-1][n-1];
    }
    public static void main(String[] args){
        Minimum_Path_Sum minimum_path_sum = new Minimum_Path_Sum();
        int[][] grid = {{7,2},{6,6},{8,6},{8,7},{5,0},{6,0}};
        System.out.println(minimum_path_sum.minPathSum(grid));
    }
}
